package com.wingice.system.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wingice.spring.security.common.utils.SecurityUtils;
import com.wingice.spring.security.model.KnifeUser;
import com.wingice.system.entity.SysOauthClientResource;
import com.wingice.system.entity.SysResource;
import com.wingice.system.mapper.SysOauthClientResourceMapper;
import com.wingice.system.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wingice.system.vo.UserRoleResourceVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色-用户关联 服务实现类
 * </p>
 *
 * @author 胡昊
 * @since 2022-01-10
 */
@Service
public class SysOauthClientResourceServiceImpl extends ServiceImpl<SysOauthClientResourceMapper, SysOauthClientResource> implements SysOauthClientResourceService {

    @Resource
    private SysResourceService sysResourceService;
    @Resource
    private SysOauthClientResourceService sysOauthClientResourceService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean saveOauthClientResource(UserRoleResourceVo userRoleResourceVo) {
        //找出现有关联
        final List<SysOauthClientResource> sysOauthClientResourceList = list(Wrappers.<SysOauthClientResource>lambdaQuery().eq(SysOauthClientResource::getOauthClientId, userRoleResourceVo.getOauthClientId()));
        //处理删除的
        final List<String> deleteIdList = sysOauthClientResourceList.stream()
                .filter(sysOauthClientResource -> !userRoleResourceVo.getResourceIdList().contains(sysOauthClientResource.getResourceId()))
                .map(SysOauthClientResource::getId)
                .collect(Collectors.toList());
        if (!CollectionUtils.isEmpty(deleteIdList)) {
            removeByIds(deleteIdList);
        }
        //处理新增的
        final List<String> resourceIdList = sysOauthClientResourceList.stream().map(SysOauthClientResource::getResourceId).collect(Collectors.toList());
        final List<String> resourceIdListNew = userRoleResourceVo.getResourceIdList().stream()
                .filter(id -> !resourceIdList.contains(id))
                .collect(Collectors.toList());
        final List<SysOauthClientResource> sysOauthClientResourceListNew = new ArrayList<>();
        resourceIdListNew.forEach(id -> {
            final SysOauthClientResource sysOauthClientResource = new SysOauthClientResource();
            sysOauthClientResource.setOauthClientId(userRoleResourceVo.getOauthClientId());
            sysOauthClientResource.setResourceId(id);
            sysOauthClientResourceListNew.add(sysOauthClientResource);
        });
        return saveBatch(sysOauthClientResourceListNew);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<SysResource> listOauthClientResource(String oauthClientId) {
        final List<String> resourceIdList = list(Wrappers.<SysOauthClientResource>lambdaQuery().eq(SysOauthClientResource::getOauthClientId, oauthClientId))
                .stream()
                .map(SysOauthClientResource::getResourceId)
                .collect(Collectors.toList());
        if (CollectionUtils.isEmpty(resourceIdList)) {
            return Collections.emptyList();
        }
        return sysResourceService.listByIds(resourceIdList);
    }
}
